//
//  YXRegistView.m
//  CreditCard
//
//  Created by 杨旭 on 2018/1/5.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXRegistView.h"

@interface YXRegistView ()<UITextFieldDelegate>

@end

@implementation YXRegistView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXRegistView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
         [self setup];
    }
    return self;
    
}
- (void)setup {
    self.scrollView.scrollEnabled = NO;
    self.bgImageView.userInteractionEnabled = YES;
    self.verificationCodeBtn.layer.masksToBounds = YES;
    self.verificationCodeBtn.layer.cornerRadius = 3.5f;
    self.registBtn.layer.masksToBounds = YES;
    self.registBtn.layer.cornerRadius = 20.0f;
    self.pidTF.delegate = self;
    self.phoneTF.delegate = self;
    self.verificationCodeTF.delegate = self;
    self.nameTF.delegate = self;
    self.passwordTF.delegate = self;
    self.password2TF.delegate = self;
    
    
    // 点击背景收回键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.scrollView addGestureRecognizer:tap];
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    [self.pidTF resignFirstResponder];
    [self.phoneTF resignFirstResponder];
    [self.verificationCodeTF resignFirstResponder];
    [self.nameTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.password2TF resignFirstResponder];

}

- (IBAction)returnBtnAction:(id)sender {
    self.returnBtnBlock();
}

- (IBAction)secureBtnAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.passwordTF.secureTextEntry = YES;
    }else if(sender.selected == NO){
        self.passwordTF.secureTextEntry = NO;
    }
}

- (IBAction)secureBtnTwoAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        self.password2TF.secureTextEntry = YES;
    }else if(sender.selected == NO){
        self.password2TF.secureTextEntry = NO;
    }
}

#pragma mark -TextField代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    return [textField resignFirstResponder];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    if (textField == self.verificationCodeTF) {
        [self setScrollViewFrame:50];
    }else if (textField == self.nameTF) {
        [self setScrollViewFrame:100];
    }else if (textField == self.passwordTF) {
        [self setScrollViewFrame:150];
    }else if (textField == self.password2TF) {
        [self setScrollViewFrame:200];
    }
 
    
}
// 让contentView上移, 防止键盘遮挡输入框
- (void)setScrollViewFrame:(CGFloat)frame {
    CGRect rect = self.scrollView.frame;
    rect.origin.y = -frame;
    self.scrollView.frame = rect;
}

// 输入结束, 键盘回收时, 下移contentView的内容视图
-(void)textFieldDidEndEditing:(UITextField *)textField {
    CGRect rect = self.scrollView.frame;
    rect.origin.y = 0;
    self.scrollView.frame = rect;
}


#pragma mark - 点击发送验证码按钮
- (IBAction)codeBtnAction:(UIButton *)sender {
    
    //获取验证码
    NSString *str = [self valiMobile:self.phoneTF.text];
    
    if(str) {
        [self showTostMessage:str];
    }else {
        [self GCDTime];
        self.verificationCodeBtnBlock(self.phoneTF.text);
    }
}
#pragma mark - 点击注册按钮
- (IBAction)registBtnAction:(UIButton *)sender {
    
    if (self.pidTF.text.length == 0 || self.phoneTF.text.length == 0 || self.verificationCodeTF.text.length == 0 || self.nameTF.text.length == 0 || self.passwordTF.text.length == 0 || self.password2TF.text.length == 0) {
        [self showTostMessage:@"请填写完整信息！"];
    }else {
        
        NSDictionary *dict = @{@"account":self.phoneTF.text,@"smsCode":self.verificationCodeTF.text,@"password":self.passwordTF.text,@"password2":self.password2TF.text,@"pid":self.pidTF.text,@"userName":self.nameTF.text};
        if (self.registBtnBlock) {
            self.registBtnBlock(dict);
        }
    }
    

}



//制作timeButton的属性文本
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time{
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc]initWithString:@"剩余" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [string4 insertAttributedString:string3 atIndex:string4.length];
    return string4;
}
#pragma mark - time
- (void)GCDTime{
    
    __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            dispatch_source_cancel(timer);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:color_blueColor}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _verificationCodeBtn.userInteractionEnabled = YES;
                [_verificationCodeBtn setAttributedTitle:string forState:(UIControlStateNormal)];
                _verificationCodeBtn.backgroundColor = [UIColor whiteColor];
//                [_verificationCodeBtn setTitleColor:color_blueColor forState:(UIControlStateNormal)];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                _verificationCodeBtn.userInteractionEnabled = NO;
                _verificationCodeBtn.backgroundColor = [UIColor py_colorWithHexString:@"#9CC4DB"];
                [_verificationCodeBtn setAttributedTitle:[self makeTimeButtonAttributed:time] forState:(UIControlStateNormal)];
                self.messageLab.text = @"已发送短信验证码到";
                self.phoneLab.text = self.phoneTF.text;
            });
            time --;
        }
    });
    dispatch_resume(timer);
}



@end
